#ifndef VIDEOCALLWIDGET_H
#define VIDEOCALLWIDGET_H

#include "qthread.h"
#include <QWidget>
#include <QMediaCaptureSession>
#include <QVideoWidget>
#include <QAudioInput>
#include <QMediaDevices>
#include <QAudioDevice>
#include <QCameraDevice>
#include <QMessageBox>
#include <QCamera>
#include <QCameraDevice>
#include <QImageCapture>
#include <QTimer>
#include <QDir>
#include <QVideoSink>
#include <QThread>
#include <QCloseEvent>
#include <QPixmap>
#include "./tools/multimedia/imagedealer.h"
#include "./tools/multimedia/videohandler.h"
#include "./tools/multimedia/audiohandler.h"
#include "./tools/network/udphandler.h"


namespace Ui {
class VideoCall;
}

class VideoCallWidget : public QWidget
{
    Q_OBJECT

public:
    explicit VideoCallWidget(QWidget *parent = nullptr);
    ~VideoCallWidget();
    void setFriendIp(const QString& userId, const QString& friendId, const QString& friendIp);

public slots:
    /* 和UDPHandler线程交互 */
    // 将对方视频渲染到客户端
    void onReceivedImage(const QImage);

private slots:
    /* 和VideoHandler线程交互 */
    // 切换摄像头开启关闭
    void onCameraStatusBtnClicked();


private:
    Ui::VideoCall *ui;

    // 负责图像处理
    QThread* m_imageDealerThread;
    ImageDealer* m_imageDealer;

    // 负责摄像头采集图像
    QThread* m_videoHandlerThread;
    VideoHandler* m_videoHandler;

    // 负责音频采集
    QThread* m_audioHandlerThread;
    AudioHandler* m_audioHandler;

    // 负责收发UDP数据
    QThread* m_UDPHandlerThread;
    UDPHandler* m_UDPHandler;


    // 当前用户视频显示控件
    QVideoWidget* m_curUserVideoWidget;
    // 朋友视频显示控件
    QVideoWidget* m_friendrVideoWidget;

    int m_shadowWidth = 8;
    int m_radius = 5;
    QPixmap m_shadowCache;
    QPixmap m_backgroundCache;

    // 维护视频开关状态
    static bool isTurnOn;

    void initUI();
    void initCameraCombobox();
    void initThread();
    void changeSelectedCamera(int index);

    void SendImage(int id, const QImage &image);

signals:
    /* 和videoHandler交互 */
    // 初始化摄像头设备
    void initCamera();
    // 改变摄像头状态
    void changeCameraStatus(bool status);
    // 切换摄像头设备
    void changeCamera(const QCameraDevice);

    /* 和ImageDealer线程交互 */
    void SendImageToDealer(const QImage image);

    /* 和主窗口交互 */
    void widgetClose();

    /* UI线程和UDPHandler线程交互 */
    // 告诉UDPHandler对方Ip
    void getFriendIp(const QString userId, const QString friendId,
                     const QString friendIp);

protected:
    void closeEvent(QCloseEvent *event) override;
    // void paintEvent(QPaintEvent *event) override;
    // void resizeEvent(QResizeEvent *event) override;

};

#endif // VIDEOCALLWIDGET_H
